{ This unit implements the bitcoin p2p protocol

  ***

  Copyright (C) 2011 Bernd Kreuss <prof7bit@googlemail.com>

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version with the following modification:

  As a special exception, the copyright holders of this library give you
  permission to link this library with independent modules to produce an
  executable, regardless of the license terms of these independent modules,and
  to copy and distribute the resulting executable under terms of your choice,
  provided that you also meet, for each linked independent module, the terms
  and conditions of the license of that module. An independent module is a
  module which is not derived from or based on this library. If you modify
  this library, you may extend this exception to your version of the library,
  but you are not obligated to do so. If you do not wish to do so, delete this
  exception statement from your version.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit bitcoin_p2p;

{$mode objfpc}{$H+}

interface
uses
  {$ifdef HEAPTRC}heaptrc,{$endif}
  Classes, SysUtils, Sockets, lNet, lCommon, dateutils, timezone, logging,
  bitcoin_crypto, bitcoin_errors, bitcoin_common, bitcoin_peerfinder;

type
  TPeer = class;

  { I have put each class along with its interface section into a separate
    include file. The defines INTERFACE and IMPLEMENTATION tell the compiler
    when to parse which part of each include (they are all included twice).
    Unlike in Delphi the code tools in the Lazarus IDE do a *phantastic*
    job of finding their way through all these files in a completely
    transparent and intuitive way. }

  {$define INTERFACE}

  // the header of all p2p messages
  {$i bitcoin_p2p_header.inc.pas}

  // some functions of which I have not
  // yet decided to where to put them
  {$i bitcoin_p2p_misc.inc.pas}

  // the base class for all p2p messages
  {$i bitcoin_p2p_msg.inc.pas}

  // these represent common data structures that
  // occur in some of the p2p messages.
  {$i bitcoin_p2p_msg_txin.inc.pas}
  {$i bitcoin_p2p_msg_txout.inc.pas}
  {$i bitcoin_p2p_msg_invvect.inc.pas}
  {$i bitcoin_p2p_msg_networkaddress.inc.pas}

  // the p2p protocol messages that are
  // received and sent over the network
  {$i bitcoin_p2p_msg_version.inc.pas}
  {$i bitcoin_p2p_msg_verack.inc.pas}
  {$i bitcoin_p2p_msg_inv.inc.pas}
  {$i bitcoin_p2p_msg_getdata.inc.pas}
  {$i bitcoin_p2p_msg_tx.inc.pas}
  {$i bitcoin_p2p_msg_addr.inc.pas}
  {$i bitcoin_p2p_msg_block.inc.pas}

  // the peer implementing some of
  // the higher protocol logic
  {$i bitcoin_p2p_peer.inc.pas}

  {$undef INTERFACE}

implementation
const
  { This array gives the factory method TMsg.GetIncomingInstance()
    the needed knowledge about what messages there actually exist
    so it can identify the incoming message and instantiate the
    appropriate class to handle it }
  MsgTypes : array[1..4] of TTMsg = (
    TMsgVersion,
    TMsgInv,
    TMsgAddr,
    TMsgBlock
  );

var
  TotalConections: Integer = 0;
  VersionNonce: QWord;


{$define IMPLEMENTATION}

  {$i bitcoin_p2p_header.inc.pas}
  {$i bitcoin_p2p_misc.inc.pas}
  {$i bitcoin_p2p_msg.inc.pas}
  {$i bitcoin_p2p_msg_txin.inc.pas}
  {$i bitcoin_p2p_msg_txout.inc.pas}
  {$i bitcoin_p2p_msg_invvect.inc.pas}
  {$i bitcoin_p2p_msg_networkaddress.inc.pas}
  {$i bitcoin_p2p_msg_version.inc.pas}
  {$i bitcoin_p2p_msg_verack.inc.pas}
  {$i bitcoin_p2p_msg_inv.inc.pas}
  {$i bitcoin_p2p_msg_getdata.inc.pas}
  {$i bitcoin_p2p_msg_tx.inc.pas}
  {$i bitcoin_p2p_msg_addr.inc.pas}
  {$i bitcoin_p2p_msg_block.inc.pas}
  {$i bitcoin_p2p_peer.inc.pas}

{$undef IMPLEMENTATION}

initialization
  Randomize;
  VersionNonce := Random(High(Int64));
end.


